
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  
<!-- Mirrored from werkzeug.palletsprojects.com/en/1.0.x/middleware/shared_data/ by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 15 Sep 2020 06:37:09 GMT -->
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Serve Shared Static Files &#8212; Werkzeug Documentation (1.0.x)</title>
    <link rel="stylesheet" href="../../_static/werkzeug.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../../../../../assets.readthedocs.org/static/css/badge_only.css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <script async="async" type="text/javascript" src="../../../../../assets.readthedocs.org/static/javascript/readthedocs-doc-embed.js"></script>
    <link rel="shortcut icon" href="../../_static/favicon.ico"/>
    <link rel="index" title="Index" href="../../genindex/index.html" />
    <link rel="search" title="Search" href="../../search/index.html" />
    <link rel="next" title="Application Dispatcher" href="../dispatcher/index.html" />
    <link rel="prev" title="X-Forwarded-For Proxy Fix" href="../proxy_fix/index.html" />
    <link rel="canonical" href="index.html">
  <script>DOCUMENTATION_OPTIONS.URL_ROOT = '../../index.html';</script>
   
  
<!-- RTD Extra Head -->

<!-- 
Always link to the latest version, as canonical.
http://docs.readthedocs.org/en/latest/canonical.html
-->
<link rel="canonical" href="index.html" />

<link rel="stylesheet" href="../../../../../assets.readthedocs.org/static/css/readthedocs-doc-embed.css" type="text/css" />

<script type="text/javascript" src="../../_static/readthedocs-data.js"></script>

<!-- Add page-specific data, which must exist in the page js, not global -->
<script type="text/javascript">
READTHEDOCS_DATA['page'] = "middleware/shared_data"
READTHEDOCS_DATA['source_suffix'] = ".rst"
</script>

<script type="text/javascript" src="../../../../../assets.readthedocs.org/static/javascript/readthedocs-analytics.js" async="async"></script>

<!-- end RTD <extrahead> -->
</head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex/index.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex/index.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../dispatcher/index.html" title="Application Dispatcher"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="../proxy_fix/index.html" title="X-Forwarded-For Proxy Fix"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">Werkzeug Documentation (1.0.x)</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" accesskey="U">Middleware</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <span class="target" id="module-werkzeug.middleware.shared_data"></span><div class="section" id="serve-shared-static-files">
<h1>Serve Shared Static Files<a class="headerlink" href="#serve-shared-static-files" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="werkzeug.middleware.shared_data.SharedDataMiddleware">
<em class="property">class </em><code class="descclassname">werkzeug.middleware.shared_data.</code><code class="descname">SharedDataMiddleware</code><span class="sig-paren">(</span><em>app</em>, <em>exports</em>, <em>disallow=None</em>, <em>cache=True</em>, <em>cache_timeout=43200</em>, <em>fallback_mimetype='application/octet-stream'</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.middleware.shared_data.SharedDataMiddleware" title="Permalink to this definition">¶</a></dt>
<dd><p>A WSGI middleware that provides static content for development
environments or simple server setups. Usage is quite simple:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">werkzeug.middleware.shared_data</span> <span class="k">import</span> <span class="n">SharedDataMiddleware</span>

<span class="n">app</span> <span class="o">=</span> <span class="n">SharedDataMiddleware</span><span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="p">{</span>
    <span class="s1">&#39;/static&#39;</span><span class="p">:</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="vm">__file__</span><span class="p">),</span> <span class="s1">&#39;static&#39;</span><span class="p">)</span>
<span class="p">})</span>
</pre></div>
</div>
<p>The contents of the folder <code class="docutils literal notranslate"><span class="pre">./shared</span></code> will now be available on
<code class="docutils literal notranslate"><span class="pre">http://example.com/shared/</span></code>.  This is pretty useful during development
because a standalone media server is not required.  One can also mount
files on the root folder and still continue to use the application because
the shared data middleware forwards all unhandled requests to the
application, even if the requests are below one of the shared folders.</p>
<p>If <cite>pkg_resources</cite> is available you can also tell the middleware to serve
files from package data:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">app</span> <span class="o">=</span> <span class="n">SharedDataMiddleware</span><span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="p">{</span>
    <span class="s1">&#39;/static&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;myapplication&#39;</span><span class="p">,</span> <span class="s1">&#39;static&#39;</span><span class="p">)</span>
<span class="p">})</span>
</pre></div>
</div>
<p>This will then serve the <code class="docutils literal notranslate"><span class="pre">static</span></code> folder in the <cite>myapplication</cite>
Python package.</p>
<p>The optional <cite>disallow</cite> parameter can be a list of <a class="reference external" href="https://docs.python.org/3/library/fnmatch.html#fnmatch.fnmatch" title="(in Python v3.8)"><code class="xref py py-func docutils literal notranslate"><span class="pre">fnmatch()</span></code></a>
rules for files that are not accessible from the web.  If <cite>cache</cite> is set to
<cite>False</cite> no caching headers are sent.</p>
<p>Currently the middleware does not support non ASCII filenames.  If the
encoding on the file system happens to be the encoding of the URI it may
work but this could also be by accident.  We strongly suggest using ASCII
only file names for static files.</p>
<p>The middleware will guess the mimetype using the Python <cite>mimetype</cite>
module.  If it’s unable to figure out the charset it will fall back
to <cite>fallback_mimetype</cite>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>app</strong> – the application to wrap.  If you don’t want to wrap an
application you can pass it <code class="xref py py-exc docutils literal notranslate"><span class="pre">NotFound</span></code>.</li>
<li><strong>exports</strong> – a list or dict of exported files and folders.</li>
<li><strong>disallow</strong> – a list of <a class="reference external" href="https://docs.python.org/3/library/fnmatch.html#fnmatch.fnmatch" title="(in Python v3.8)"><code class="xref py py-func docutils literal notranslate"><span class="pre">fnmatch()</span></code></a> rules.</li>
<li><strong>cache</strong> – enable or disable caching headers.</li>
<li><strong>cache_timeout</strong> – the cache timeout in seconds for the headers.</li>
<li><strong>fallback_mimetype</strong> – The fallback mimetype for unknown files.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.0: </span>The default <code class="docutils literal notranslate"><span class="pre">fallback_mimetype</span></code> is
<code class="docutils literal notranslate"><span class="pre">application/octet-stream</span></code>. If a filename looks like a text
mimetype, the <code class="docutils literal notranslate"><span class="pre">utf-8</span></code> charset is added to it.</p>
</div>
<details class="changelog">
<summary>Changelog</summary><div class="versionadded">
<p><span class="versionmodified">New in version 0.6: </span>Added <code class="docutils literal notranslate"><span class="pre">fallback_mimetype</span></code>.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.5: </span>Added <code class="docutils literal notranslate"><span class="pre">cache_timeout</span></code>.</p>
</div>
</details><dl class="method">
<dt id="werkzeug.middleware.shared_data.SharedDataMiddleware.is_allowed">
<code class="descname">is_allowed</code><span class="sig-paren">(</span><em>filename</em><span class="sig-paren">)</span><a class="headerlink" href="#werkzeug.middleware.shared_data.SharedDataMiddleware.is_allowed" title="Permalink to this definition">¶</a></dt>
<dd><p>Subclasses can override this method to disallow the access to
certain files.  However by providing <cite>disallow</cite> in the constructor
this method is overwritten.</p>
</dd></dl>

</dd></dl>

</div>


          </div>
        </div>
      </div>
  <span id="sidebar-top"></span>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  
    
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/werkzeug.png" alt="Logo"/>
            </a></p>
  
<h3>Navigation</h3>
<ul>
  <li><a href="../../index.html">Overview</a>
    <ul>
      <li><a href="../index.html">Middleware</a>
        <ul>
          <li>Previous: <a href="../proxy_fix/index.html" title="previous chapter">X-Forwarded-For Proxy Fix</a>
          <li>Next: <a href="../dispatcher/index.html" title="next chapter">Application Dispatcher</a></ul>
      </li>
    </ul>
  </li>
</ul>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://werkzeug.palletsprojects.com/en/1.0.x/search/" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
  
    <div class="footer" role="contentinfo">
        &#169; Copyright 2007 Pallets.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.8.5.
    </div>
    <script>
      if (typeof READTHEDOCS_DATA !== 'undefined') {
        if (!READTHEDOCS_DATA.features) {
          READTHEDOCS_DATA.features = {};
        }
        READTHEDOCS_DATA.features.docsearch_disabled = true;
      }
    </script>

  </body>

<!-- Mirrored from werkzeug.palletsprojects.com/en/1.0.x/middleware/shared_data/ by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 15 Sep 2020 06:37:09 GMT -->
</html>